using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using ISXWoW;
using LavishVMAPI;
using InnerSpaceAPI;

namespace ISXPallyAssist
{
    public partial class ISXPallyAssist : Form
    {
        public ISXPallyAssist()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //ASSIST START
            ParameterizedThreadStart p = new ParameterizedThreadStart(RunPallyAssist);
            Thread t = new Thread(p);
            assistingCheckBox.Checked = true;
            string[] q = { "Q" };
            t.Start(q);
        }

        private void RunPallyAssist(object p)
        {
            // PALLY ASSIST MAIN THREAD
            ISXWoW.ISXWoW isxwow = new ISXWoW.ISXWoW();

            while (assistingCheckBox.Checked)
            {
                bool isDead = isxwow.Me.Dead;
                if (!isDead)
                {
                    CheckBuffs();
                    CheckTargetDistance();
                    DoCombat();
                    //DoLoot();
                }
            }
        }

        private void DoLoot()
        {
            ISXWoW.ISXWoW isxwow = new ISXWoW.ISXWoW();
            GuidList gList = new GuidList();
            gList.Search("-noself", "-range 0-5");
            for (uint i = 0; i < gList.Count; i++)
            {
                if (gList.Object(i).GetUnit().Lootable)
                {
                    LavishScriptAPI.LavishScript.ExecuteCommand(String.Format("press -hold shift"));
                    gList.Object(i).GetUnit().Use();
                    LavishScriptAPI.LavishScript.ExecuteCommand(String.Format("press -release shift"));
                }
            }
        }

        private void CheckTargetDistance()
        {
            ISXWoW.ISXWoW isxwow = new ISXWoW.ISXWoW();
            bool hasTarget = isxwow.Me.CurrentTarget.IsValid;
            bool targetIsDead = isxwow.Me.CurrentTarget.Dead;
            if (hasTarget && !targetIsDead)
            {
                double targetDistance = isxwow.Me.CurrentTarget.Distance;
                if (targetDistance > 4)
                {
                    DoPreCombatHPCheck();
                    while (targetDistance > 4 && hasTarget && !targetIsDead)
                    {
                        targetIsDead = isxwow.Me.CurrentTarget.Dead;
                        hasTarget = isxwow.Me.CurrentTarget.IsValid;
                        targetDistance = isxwow.Me.CurrentTarget.Distance;
                        isxwow.Me.CurrentTarget.Face();
                        WoWMovement movement = new WoWMovement();
                        movement.Go(MovementDirection.Forward);
                    }
                    LavishScriptAPI.LavishScript.ExecuteCommand(String.Format("move -stop")); // stop!
                }
            }
        }

        private void DoPreCombatHPCheck()
        {
            ISXWoW.ISXWoW isxwow = new ISXWoW.ISXWoW();
            bool attacking = isxwow.Me.Attacking;
            double myHPPct = isxwow.Me.HealthPercent;
            if(!attacking)
            {
                if (myHPPct < 50)
                {
                    while (myHPPct < 50)
                    {
                        bool casting = isxwow.Me.IsCasting;
                        if(!casting)
                            Cast("Holy Light");
                        myHPPct = isxwow.Me.HealthPercent;
                    }
                }
            }
        }

        private void CheckBuffs()
        {
            ISXWoW.ISXWoW isxwow = new ISXWoW.ISXWoW();
            bool attacking = isxwow.Me.Attacking;
            bool devotionAura = isxwow.Me.Buff("Devotion Aura").IsValid;
            bool BoM = isxwow.Me.Buff("Blessing of Wisdom").IsValid;
            if (!attacking)
            {
                if (!BoM)
                    Cast("Blessing of Wisdom");
                if (!devotionAura)
                    Cast("Devotion Aura");
            }
        }

        private void DoCombat()
        {
            ISXWoW.ISXWoW isxwow = new ISXWoW.ISXWoW();
            bool hasTarget = isxwow.Me.CurrentTarget.IsValid;
            bool targettingMe = isxwow.Me.CurrentTarget.CurrentTarget.Equals(isxwow.Me);
            bool targetAggressive = isxwow.Me.CurrentTarget.Aggressive;
            double targetDist = isxwow.Me.CurrentTarget.Distance;

            if (hasTarget && targetDist < 5 && targettingMe && !targetAggressive)
            {
                isxwow.Me.Action("Attack").Use();
            }

            if (hasTarget && targetAggressive)
            {
                GetAtRange();
                int targetCount = EnemyCount();
                isxwow.Me.CurrentTarget.Face(); // face the target
                double targetHPPct = isxwow.Me.CurrentTarget.HealthPercent;
                bool targetHasHoJ = isxwow.Me.CurrentTarget.Buff("Hammer of Justice").IsValid;
                double myHPPct = isxwow.Me.HealthPercent;
                bool meCasting = isxwow.Me.IsCasting;
                bool isAutoAtacking = isxwow.Me.Attacking;
                bool noConsCD = isxwow.Me.Action("Consecration").Usable;
                bool noHoJCD = isxwow.Me.Action("Hammer of Justice").Usable;
                bool hasDivine = isxwow.Me.Buff("Divine Protection").IsValid;
                bool noDivineCD = isxwow.Me.Action("Divine Shield").Usable;
                bool noBlessofProtCD = isxwow.Me.Action("Blessing of Protection").Usable;
                bool cannotShield = isxwow.Me.Buff("Forbearance").IsValid;
                bool hasBlessOfProt = isxwow.Me.Buff("Blessing of Protection").IsValid;
                bool noJudgementCD = isxwow.Me.Action("Judgement").Usable;
                bool hasSoC = isxwow.Me.Buff("Seal of Command").IsValid;
                float SoCDuration = isxwow.Me.Buff("Seal of Command").Duration;

                if (!meCasting)
                {
                    if (myHPPct <= 10 && noDivineCD &&!cannotShield)
                    {
                        Cast("Divine Protection");
                    }
                    if (myHPPct <= 10 && !cannotShield && !noDivineCD)
                    {
                        Cast("Blessing of Protection");
                    }
                    if (hasDivine && myHPPct < 80)
                    {
                        Cast("Holy Light");
                    }
                    if (hasBlessOfProt && myHPPct < 80)
                    {
                        Cast("Holy Light");
                    }
                    if (!hasSoC && targetHPPct > 10)
                    {
                        Cast("Seal of Command");
                    }
                    if (myHPPct < 10 && targetHasHoJ)
                    {
                        Cast("Holy Light");
                    }
                    if (hasSoC && targetHasHoJ && noJudgementCD)
                    {
                        Cast("Judgement");
                    }
                    if (hasSoC && !noHoJCD && targetHPPct < 10 && noJudgementCD)
                    {
                        Cast("Judgement");
                    }
                    if ((myHPPct < 10 && !noDivineCD && !noBlessofProtCD && noHoJCD) || (myHPPct < 10 && cannotShield && noHoJCD))
                    {
                        Cast("Hammer of Justice");
                    }
                    if (targetHPPct < 20 && noHoJCD)
                    {
                        Cast("Hammer of Justice");
                    }
                    if (SoCDuration < 3 && noHoJCD)
                    {
                        Cast("Hammer of Justice");
                    }
                    if (SoCDuration < 3 && !noHoJCD && noJudgementCD)
                    {
                        Cast("Judgement");
                    }
                    if (myHPPct < 10 && !noDivineCD && !noBlessofProtCD && !noHoJCD)
                    {
                        Cast("Lay on Hands");
                    }
                    if (targetCount > 1 && noConsCD)
                    {
                        Cast("Consecration");
                    }

                    //DoDispellings();

                    if (!isAutoAtacking && !hasBlessOfProt && !hasDivine)
                    {
                        isxwow.Me.Action("Attack").Use();
                    }                    
                }
            }           
        }

        private void GetAtRange()
        {
            ISXWoW.ISXWoW isxwow = new ISXWoW.ISXWoW();
            bool hasTarget = isxwow.Me.CurrentTarget.IsValid;
            bool targetIsDead = isxwow.Me.CurrentTarget.Dead;
            if (hasTarget && !targetIsDead)
            {
                double targetDistance = isxwow.Me.CurrentTarget.Distance;
                if (targetDistance < 2)
                {
                    DoPreCombatHPCheck();
                    while (targetDistance < 2 && hasTarget && !targetIsDead)
                    {
                        targetIsDead = isxwow.Me.CurrentTarget.Dead;
                        hasTarget = isxwow.Me.CurrentTarget.IsValid;
                        targetDistance = isxwow.Me.CurrentTarget.Distance;
                        isxwow.Me.CurrentTarget.Face();
                        WoWMovement movement = new WoWMovement();
                        movement.Go(MovementDirection.Backward);
                    }
                    LavishScriptAPI.LavishScript.ExecuteCommand(String.Format("move -stop")); // stop!
                }
            }
        }

        private void DoDispellings()
        {
            ISXWoW.ISXWoW isxwow = new ISXWoW.ISXWoW();
            isxwow.WoWScript("Decursive()");
        }

        private void Cast(string p)
        {
            ISXWoW.ISXWoW isxwow = new ISXWoW.ISXWoW();
            WoWSpell.Get(p).Cast();
        }

        private int EnemyCount()
        {
            ISXWoW.ISXWoW isxwow = new ISXWoW.ISXWoW();
            GuidList gList = new GuidList();
            gList.Search("-noself", "-alive", "-hostile");
            int rVar = 0;
            for (uint i = 0; i < gList.Count; i++)
            {
                if (gList.Object(i).GetUnit().CurrentTarget.Equals(isxwow.Me))
                    rVar++;
            }

            return rVar;
        }
    }
}